<template>
  <div class="login-container">
    <el-form
      autoComplete="on"
      v-bind:model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-position="left"
      label-width="0px"
      class="login-form"
    >
      <img class="m-img" src="../../assets/badge.png" alt="">
      <h3 class="title">淮安公安互联网报警服务平台</h3>
      <el-form-item prop="tenantId" v-show="false">
        <el-input size="small"
                  @keyup.enter.native="handleLogin"
                  v-model="loginForm.tenantId"
                  auto-complete="off"
                  :placeholder="$t('login.tenantId')">
          <i slot="prefix"
             class="icon-quanxian"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="username">
        <el-input size="small"
                  @keyup.enter.native="handleLogin"
                  v-model="loginForm.username"
                  auto-complete="off"
                  :placeholder="$t('login.username')">
          <i slot="prefix"
             class="icon-yonghu"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input size="small"
                  @keyup.enter.native="handleLogin"
                  :type="passwordType"
                  v-model="loginForm.password"
                  auto-complete="off"
                  :placeholder="$t('login.password')">
          <i class="el-icon-view el-input__icon"
             slot="suffix"
             @click="showPassword"></i>
          <i slot="prefix"
             class="icon-mima"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-row :span="24">
          <el-col :span="16">
            <el-input size="small"
                      @keyup.enter.native="handleLogin"
                      v-model="loginForm.code"
                      auto-complete="off"
                      :placeholder="$t('login.code')">
              <i slot="prefix" class="icon-yanzhengma"/>
            </el-input>
          </el-col>
          <el-col :span="8">
            <div class="login-code">
              <img :src="loginForm.image" class="login-code-img" @click="refreshCode"
              />
            </div>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   style="width:100%;"
                   @click.native.prevent="handleLogin"
                   class="login-submit">{{$t('login.submit')}}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getCaptcha} from "@/api/user";
import {mapGetters} from "vuex";
export default {
  name: "login",
  components: {},
  data() {
    return {
      loginForm: {
        tenantId: "586143",
        // tenantId: "000000",
        //用户名
        username: "admin",
        //密码
        password: "123456",
        //账户类型
        type: "account",
        //验证码的值
        code: "",
        //验证码的值
        deviceId: "28C6D2CB-9119-43DC-B720-0306FD4662A0",
        //验证码的索引
        key: "",
        //预加载白色背景
        image: "",
      },
      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }, {min: 1, message: "密码长度最少为6位", trigger: "blur"}]
      },
      loading: false,
      passwordType: "password"
    };
  },
  computed: {
    ...mapGetters(["tagWel"])
  },
  mounted() {
    this.refreshCode();
  },
  methods: {
    showPassword() {
      this.passwordType === ""
        ? (this.passwordType = "password")
        : (this.passwordType = "");
    },
    refreshCode() {
      getCaptcha().then(res => {
        const data = res.data.data;
        this.loginForm.key = data.key;
        this.loginForm.image = data.image;
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '登录中,请稍后。。。',
            spinner: "el-icon-loading"
          });
          this.$store.dispatch("LoginByUsername", this.loginForm).then(() => {
            this.$store.dispatch("GetCurrentUserInfo").then(() => {
              this.$router.push({path: this.tagWel.value});
            })
            loading.close();
          }).catch(() => {
            loading.close()
          });
        }
      });
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {

  height: 100vh;
  background-image: url("../../assets/timgb.png");
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .login-form {
    box-sizing: border-box;
    width: 400px;
    padding: 35px 0px 120px 0px;
    text-align: center;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
  }

  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 0px;
    color: $light_gray;
    height: 47px;
  }

  .m-img{
    width: 174px;
    height: 176px;
    margin: 0px 0px 0px 0px;
  }

  .title {
    font-size: 26px;
    color: $light_gray;
    margin: 10px 0px 40px 0px;
    text-align: center;
    font-weight: bold;
  }
}
.login-code {
  display: flex;
  align-items: center;
  justify-content: right;
  margin: 0 0 0 10px;
}
.login-code-img {
  width: 100px;
  height: 38px;
  cursor:pointer!important;
}
.el-col {
  margin-bottom: 0px !important;
}
</style>
